package com.situ.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.ToggleButton;

import androidx.annotation.Nullable;

public class ToggleActivity extends Activity {
    private ToggleButton toggleButton;
    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_toggle);

        toggleButton = findViewById(R.id.btn_toggle);
        imageView = findViewById(R.id.imageView);

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 根据ToggleButton当前状态，改变ImageView显示图片
                // isChecked代表当前ToggleButton当前状态是开还是关
                if (isChecked) {
                    // 用代码改变ImageView的背景图片
                    imageView.setBackgroundResource(R.mipmap.on);
                } else {
                    imageView.setBackgroundResource(R.mipmap.off);
                }
            }
        });

    }
}
